// orderPackage/pages/returnGoods/returnGoods.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    applyFlag: 1, // 1 提交退货申请   2 完善申请信息
    selectNum: 1, //申请的数量
    basicSocre: 0, //  基础积分 平均的
    basicMoney: 0, //  基础余额


    order: '', //退货的订单
    resType: '退款退货', // 申请类型  
    resTypeId: -1, // 申请类型 标识    1 退款退货  2 仅退款
    reason: '', // 退货原因
    reasonText: '', // 原因具体描述
    backWay: '自行寄回', //寄回方式
    returnMoney: 0, //退款金额
    showReason: false,
    showType: false,
    // 申请退货理由
    reasonList: [{
        text: '商品损坏',
        isSelect: false
      },
      {
        text: '少/错商品',
        isSelect: false
      },
      {
        text: '发错货',
        isSelect: false
      },
      {
        text: '商品与页面描述不符',
        isSelect: false
      },
      {
        text: '质量问题',
        isSelect: false
      },
      {
        text: '其他',
        isSelect: false
      },
    ],
    show: false, // 显示 寄回方式
    // 寄回方式
    actions: [{
        name: '上门取件',
      },
      {
        name: '自行寄回',
      }
    ],
    // 申请类型方式
    typeActions: [{
        id: 1,
        name: '退款退货',
      },
      {
        id: 2,
        name: '仅退款',
      }
    ],

    backInfo: { //  回寄的地址信息
      name: '某某某',
      tel: '15904440357',
      address: '长春市吉林大学第一医院二部3号 楼4楼201室s'

    },
    showWuliu: false, // 显示  物流弹窗
    arr: [{
        code: 'SF',
        text: '顺丰'
      },
      {
        code: 'STO',
        text: '申通'
      },
      {
        code: 'YTO',
        text: '圆通'
      },
      {
        code: 'YD',
        text: '韵达'
      },
      {
        code: 'HHTT',
        text: '天天'
      },
      {
        code: 'EMS',
        text: 'EMS'
      },
      {
        code: 'ZTO',
        text: '中通'
      },
      {
        code: 'DBL',
        text: '德邦'
      },
      {
        code: 'JD',
        text: '京东快递'
      },
      {
        code: 'ZJS',
        text: '宅急送'
      },
      {
        code: 'FEDEX_GJ',
        text: 'Fedex国际'
      },
      {
        code: 'UPS',
        text: 'UPS国际快递'
      },
      {
        code: 'SURE',
        text: '速尔快递'
      },
      {
        code: 'XFEX',
        text: '信丰物流'
      },
      {
        code: 'UC',
        text: '优速快递'
      },
      {
        code: 'CNPEX',
        text: '中邮物流'
      },
      {
        code: 'AAE',
        text: 'AAE全球专递'
      },
      {
        code: 'DHL',
        text: 'DHL'
      },
      {
        code: 'FEDEX_GJ',
        text: 'DPEX国际快递'
      },
      {
        code: 'DSWL',
        text: 'D速物流'
      },
      {
        code: 'FEDEX',
        text: 'FEDEX国内快递'
      },
      {
        code: 'OCS',
        text: 'OCS'
      },
      {
        code: 'TNT',
        text: 'TNT'
      },
      {
        code: 'ycky',
        text: '远成快运'
      },
      {
        code: 'YZPY',
        text: '邮政快递'
      },
      {
        code: 'HTKY',
        text: '百世快运'
      }
    ],


    backWuLiuInfo: {
      orderNum: '', // 物流单号
      wlCom: '', // 物流公司
      wlCode: '', // 物流公司 编码
    }


  },


  // 商品数量
  onNumChange(e) {
    this.setData({
      selectNum: e.detail
    })
    console.log(this.data);
  },


  // 提交基本信息
  submitHandle() {

    var applyFlag = this.data.applyFlag,
      reason = this.data.reason,
      resType = this.data.resType,
      order = this.data.order,
      backWuLiuInfo = this.data.backWuLiuInfo,
      that = this,
      reasonText = this.data.reasonText;
    if (applyFlag == 1) {
      if (resType == '') {
        wx.showToast({
          title: '请选择申请类型',
          icon: 'none'
        })
        return
      }
      if (reason == '') {
        wx.showToast({
          title: '请选择申请原因',
          icon: 'none'
        })
        return
      }
      if (reasonText == '') {
        wx.showToast({
          title: '请输入具体的问题描述',
          icon: 'none'
        })
        return
      }

      var obj = {
        infoId: order.infoId,
        orderNumber: order.orderNumber,
        returnNum: that.data.selectNum,
        totalMoney: order.orderTotalMoney, //退货商品所在订单 总金额
        returnStatus: 0,
        userId: app.globalData.userId,
        returnReason: reason,
        returnDetail: reasonText,
        returnBalance: that.data.selectNum * that.data.basicMoney,
        returnScore: that.data.selectNum * that.data.basicSocre,
        returnMoney: that.data.selectNum * order.productPrice,

      }

      console.log(obj);
      // return

      this.submitInfoHandle(obj)

    } else {
      if (backWuLiuInfo.wlCom == '') {
        wx.showToast({
          title: '请选择物流公司',
          icon: 'none'
        })
        return
      }
      if (backWuLiuInfo.wlCom != '' && backWuLiuInfo.orderNum == '') {
        wx.showToast({
          title: '请输入快递单号',
          icon: 'none'
        })
        return
      }

      var obj = {
        returnId: order.returnId,
        infoId: order.infoId,
        orderNumber: order.orderNumber,
        returnNum: order.returnNum,
        returnStatus: order.returnStatus,
        userId: app.globalData.userId,
        returnReason: reason,
        returnDetail: reasonText,
        returnKdCode: backWuLiuInfo.orderNum || '',
        returnKd: backWuLiuInfo.wlCom || '',
        returnBalance: order.returnBalance,
        returnScore: order.returnScore,
        returnMoney: order.returnMoney,
        totalMoney: order.totalMoney
      }

      this.submitInfoHandle(obj)
    }

  },


  // 提交退货申请
  submitInfoHandle(obj) {

    console.log(obj);


    var applyFlag = this.data.applyFlag;


    wx.showLoading({
      title: '',
    })
    var url = app.globalData.url + "ScOrder/returnOrder";

    app.wxRequest(url, obj, res => {
        console.log(res);
        if (res.flag) {
          wx.showModal({
            title: '提示',
            content: applyFlag == 1 ? '已提交退货申请' : '信息提交成功，请耐心等待',
            showCancel: false,
            complete: (res) => {
              if (res.confirm) {
                wx.navigateBack()
              }
            }
          })
        }
      },
      err => {
        console.log(err);
      })







  },




  onConfirm(e) {
    console.log(e);
    var str1 = 'backWuLiuInfo.wlCom',
      str2 = 'backWuLiuInfo.wlCode'
    this.setData({
      [str1]: e.detail.value.text,
      [str2]: e.detail.value.code,
      showWuliu: false
    })
  },
  onCancel() {
    this.setData({
      showWuliu: false
    })
  },

  // 复制地址
  copyHandle() {
    var info = this.data.backInfo,
      str = '';
    str = info.name + ' ' + info.tel + ' ' + info.address
    wx.setClipboardData({ //设置系统剪贴板的内容
      data: str,
      success(res) {
        wx.getClipboardData({ // 获取系统剪贴板的内容
          success(res) {
            wx.showToast({
              title: '复制成功',
              icon: 'none'
            })
          }
        })
      }
    })

  },

  showHandle(e) {
    var flag = e.currentTarget.dataset.flag;
    if (flag == 'reason') {
      if (this.data.applyFlag == 1) {
        this.setData({
          showReason: true
        })
      }
    } else if (flag == 'backWay') {
      wx.showToast({
        title: '只支持自行寄回，请见谅',
        icon: 'none'
      })
      return
      this.setData({
        show: true
      })
    } else if (flag == 'wuLiu') {
      this.setData({
        showWuliu: true
      })
    } else if (flag == 'type') {
      return
      this.setData({
        showType: true
      })

    }
  },


  // 保存退货原因
  saveReason() {
    var reasonList = this.data.reasonList,
      reason = this.data.reason,
      arr = reasonList.filter(item => {
        return item.isSelect
      })

    if (arr.length == 0) {
      wx.showToast({
        title: '请选择退货原因',
        icon: 'none'
      })
      return
    }

    reason = arr[0].text

    this.setData({
      reason,
      showReason: false
    })

  },
  // 选择寄回方式
  onSelect(e) {
    console.log(e);
    this.setData({
      backWay: e.detail.name,
      show: false
    })
  },

  onTypeSelect(e) {
    console.log(e);
    this.setData({
      resType: e.detail.name,
      resTypeId: e.detail.id,
      showType: false
    })
  },

  onTypeClose() {
    this.setData({
      showType: false
    })
  },

  // 选择退货原因
  seletReason(e) {
    console.log(e);
    var index = e.currentTarget.dataset.index,
      reasonList = this.data.reasonList;

    reasonList.forEach((ele, eIndex) => {
      ele.isSelect = index == eIndex
    })
    this.setData({
      reasonList
    })


  },
  onCloseReason() {
    this.setData({
      showReason: false
    })
  },
  onClose() {
    this.setData({
      show: false
    })
  },

  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag
    var val = e.detail.value
    this.setData({
      [str]: val
    })
    console.log(this.data);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var basicSocre = 0, //  基础积分 平均的
      basicMoney = 0,
      order = JSON.parse(options.order); //  基础余额
    console.log(options);

    basicSocre = Math.floor(order.deductScore / order.productNum)
    basicMoney = Math.floor(order.deductMoney / order.productNum)

    console.log(basicSocre);
    console.log(basicMoney);



    this.setData({
      order,
      applyFlag: options.flag,
      basicSocre,
      basicMoney
    })


    if (this.data.applyFlag == 2) {
      var order = this.data.order,
        reason = order.returnReason,
        reasonText = order.returnDetail,
        returnMoney = order.returnMoney;

      this.setData({
        reason,
        reasonText,
        returnMoney
      })


    }

    console.log(this.data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})